package servlet.admin;

import entity.User;
import service.Impl.UserServiceImpl;
import service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/admin/UserEdit")
public class UserEditServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        Integer id = Integer.parseInt(req.getParameter("id"));
        String userName = req.getParameter("userName");
        String password = req.getParameter("password");
        String sex = req.getParameter("sex");
        String ageStr = req.getParameter("age");
        String telephone = req.getParameter("telephone");
        String email = req.getParameter("email"); // 确保这个参数被正确获取
        String address = req.getParameter("address");
        String pic = req.getParameter("pic"); // 确保这个参数被正确获取
        String stateStr = req.getParameter("state");
        User user = new User();
        user.setId(id);
        user.setUserName(userName);
        user.setPassword(password);
        user.setSex(sex);
        user.setAge(Integer.parseInt(ageStr)); // 确保 ageStr 不为空
        user.setTelephone(telephone);
        user.setEmail(email); // 确保这个字段被设置
        user.setAddress(address);
        user.setPic(pic); // 确保这个字段被设置
        user.setState(Integer.parseInt(stateStr)); // 确保 stateStr 不为空
        userService.updateUser(user);
        resp.sendRedirect(req.getContextPath()+"/admin/UserList");
    }
}
